using System;
using System.Collections.Generic;
using System.Text;
using Sucrose.Base;
using Sucrose.Output;

namespace Sucrose
{
    public class SucroseControl
    {
        private static SucroseControl instance = null;

        public event ProgressUpdateHandler ProgressUpdates;

        public static SucroseControl GetInstance()
        {
            if (instance == null)
                instance = new SucroseControl();
            return instance;
        }

        #region settingsProperties

        public bool DownloadSupportDocuments
        {
            set
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                output.DownloadSupportDocuments = value;
            }
            get
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                return output.DownloadSupportDocuments;
            }
        }

        public bool DownloadTaskDocuments
        {
            set
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                output.DownloadTaskDocuments = value;
            }
            get
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                return output.DownloadTaskDocuments;
            }
        }

        public bool ImportGrades
        {
            set
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                output.ImportGrades = value;
            }
            get
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                return output.ImportGrades;
            }
        }

        public bool ImportTasks
        {
            set
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                output.ImportTasks = value;
            }
            get
            {
                SucroseOutputControl output = SucroseOutputControl.GetInstance();
                return output.ImportTasks;
            }
        }

        public int UpdateInterval
        {
            get
            {
                int interval = 60;
                try
                {
                    interval = Settings.GetIntegerValueForKey("UpdateInterval");
                }
                catch(KeyNotFoundException)
                {
                    UpdateInterval = 60;
                    interval = 60;
                }

                return interval;
            }
            set
            {
                if (value > 5)
                {
                    Settings.SetValueAtKey("UpdateInterval", value);
                }
                else
                {
                    throw new IndexOutOfRangeException();
                }
            }
        }

        public bool AutoUpdate 
        {
            get
            {
                bool auto = true;
                try
                {
                    int autoUpdates = Settings.GetIntegerValueForKey("AutoUpdate");
                    auto = (autoUpdates == 1);
                }
                catch (KeyNotFoundException)
                {
                    AutoUpdate = true;
                    auto = true;
                }

                return auto;
            }
            set
            {
                if (value == true)
                    Settings.SetValueAtKey("AutoUpdate", 1);
                else
                    Settings.SetValueAtKey("AutoUpdate", 0);
            }
        }

        #endregion

        public SucroseControl()
        {
            this.InitializeSettings();

            SucroseOutputControl output = SucroseOutputControl.GetInstance();
            SucroseBaseControl import = SucroseBaseControl.GetInstance();

            output.ProgressUpdates += new ProgressUpdateHandler(ProgressUpdateRelay);
            import.ProgressUpdates += new ProgressUpdateHandler(ProgressUpdateRelay);
        }

        /// <summary>
        /// Convenience method. Both imports from the LMS and exports to Outlook.
        /// </summary>
        public void DoAll()
        {
            SucroseOutputControl output = SucroseOutputControl.GetInstance();
            SucroseBaseControl import = SucroseBaseControl.GetInstance();

            try
            {
                import.ImportCourses();
                output.ExportCourses(import.Courses);
            }
            catch (ActionCancelledException) { }
        }

        /// <summary>
        /// Forwards the update events to Control update events.
        /// </summary>
        /// <param name="currentTicks">Numerator of percent complete fraction</param>
        /// <param name="totalTicks">Denominator of percent complete fraction</param>
        private void ProgressUpdateRelay(int currentTicks, int totalTicks)
        {
            if (ProgressUpdates != null)
                ProgressUpdates.Invoke(currentTicks, totalTicks);
        }

        public void InitializeSettings()
        {
            SucroseOutputControl output = SucroseOutputControl.GetInstance();
            SucroseBaseControl import = SucroseBaseControl.GetInstance();

            output.InitializeOutputSettings();
            //Initialize settings for the import too

            PrepareFirstRun();
        }

        public void PrepareFirstRun()
        {
            bool firstRun = IsFirstRun();

            if (firstRun)
            {
                String defaultPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Sucrose\\";

                SucroseBaseControl import = SucroseBaseControl.GetInstance();
                import.SupportPath = defaultPath;

                Settings.SetValueAtKey("version", "4.1.1");
            }
        }

        public bool IsFirstRun()
        {
            try
            {
                String version = Settings.GetValueForKey("version");

                if (version == "4.1.1")
                    return false;
                else
                    return true;
            }
            catch (KeyNotFoundException)
            {
                return true;
            }
        }
    }
}
